package com.ruoyi.creditlife.service.impl;

import com.ruoyi.creditlife.domain.ComplianceRecord;
import com.ruoyi.creditlife.mapper.ComplianceRecordMapper;
import com.ruoyi.creditlife.service.IComplianceRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 用户守约记录Service业务层处理
 * 
 * @author ddd
 * @date 2025-06-30
 */
@Service
public class ComplianceRecordServiceImpl implements IComplianceRecordService 
{
    @Autowired
    private ComplianceRecordMapper complianceRecordMapper;

    @Override
    public int countComplianceRecords(Long userId) {
        ComplianceRecord param = new ComplianceRecord();
        param.setUserId(userId);
        return complianceRecordMapper.selectComplianceRecordList(param).size();
    }

    @Override
    public double getRecentComplianceRate(Long userId, int months) {
        // 这里实现您的守约率计算逻辑
        // 示例：查询近几个月的守约记录和违约记录，计算比率
        int compliantCount = complianceRecordMapper.countRecentCompliantRecords(userId, months);
        int totalCount = complianceRecordMapper.countRecentRecords(userId, months);
        return totalCount > 0 ? (compliantCount * 100.0 / totalCount) : 100.0;
    }

    @Override
    public List<Map<String, Object>> getTypeDistribution(Long userId) {
        return complianceRecordMapper.selectTypeDistributionByUserId(userId);
    }

    /**
     * 查询用户守约记录
     * 
     * @param id 用户守约记录主键
     * @return 用户守约记录
     */
    @Override
    public ComplianceRecord selectComplianceRecordById(Long id)
    {
        return complianceRecordMapper.selectComplianceRecordById(id);
    }

    /**
     * 查询用户守约记录列表
     * 
     * @param complianceRecord 用户守约记录
     * @return 用户守约记录
     */
    @Override
    public List<ComplianceRecord> selectComplianceRecordList(ComplianceRecord complianceRecord)
    {
        return complianceRecordMapper.selectComplianceRecordList(complianceRecord);
    }

    /**
     * 新增用户守约记录
     * 
     * @param complianceRecord 用户守约记录
     * @return 结果
     */
    @Override
    public int insertComplianceRecord(ComplianceRecord complianceRecord)
    {
        return complianceRecordMapper.insertComplianceRecord(complianceRecord);
    }

    /**
     * 修改用户守约记录
     * 
     * @param complianceRecord 用户守约记录
     * @return 结果
     */
    @Override
    public int updateComplianceRecord(ComplianceRecord complianceRecord)
    {
        return complianceRecordMapper.updateComplianceRecord(complianceRecord);
    }

    /**
     * 批量删除用户守约记录
     * 
     * @param ids 需要删除的用户守约记录主键
     * @return 结果
     */
    @Override
    public int deleteComplianceRecordByIds(Long[] ids)
    {
        return complianceRecordMapper.deleteComplianceRecordByIds(ids);
    }

    /**
     * 删除用户守约记录信息
     * 
     * @param id 用户守约记录主键
     * @return 结果
     */
    @Override
    public int deleteComplianceRecordById(Long id)
    {
        return complianceRecordMapper.deleteComplianceRecordById(id);
    }
}
